package com.lanou.dllo.findfood.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.dllo.findfood.entity.HomeLastResult;
import com.lanou.dllo.findfood.util.Constant;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class MySQLHomeRecyeBaseUtil {
    private SQLiteDatabase database;

    public MySQLHomeRecyeBaseUtil(Context context) {
        init(context);
    }

    public void init(Context context) {
//factory 游标, version
        //构建数据库帮助类用于数据库对象的获取
        //参数二表示数据库名
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        database = helper.getWritableDatabase();
    }

    //以键值对的形式保存
    public void savaHomeRecylerView(List<HomeLastResult> results) {
        for (HomeLastResult result : results) {
            ContentValues values = new ContentValues();
           values.put(Constant.COLUMN_HOME_RECYLECLER_COMMENT, result.getComment());
            values.put(Constant.COLUMN_HOME_RECYLECLER_PLACE_NAME, result.getPlace_name());
            values.put(Constant.COLUMN_HOME_RECYLECLER_USER_NAME, result.getUser_name());
            values.put(Constant.COLUMN_HOME_RECYLECLER_FOOD_NAME, result.getFood_name());
            values.put(Constant.COLUMN_HOME_RECYLECLER_PICTURE_URL, result.getPicture_url());
            values.put(Constant.COLUMN_HOME_RECYLECLER_USER_IMAGE, result.getUser_image());
         //   values.put(Constant.COLUMN_HOME_RECYLECLER_USER_ID,result.getUser_id());
            database.insert(Constant.TABLE_NAME_HOME_RECYLECLER, null, values);


        }
    }


    public List<HomeLastResult> queryHomeRecylerData() {

        Cursor cursor = database.query(Constant.TABLE_NAME_HOME_RECYLECLER, null,
                null, null, null, null, null);
        if (cursor != null) {
            List<HomeLastResult> results = new ArrayList<HomeLastResult>();
            while (cursor.moveToNext()) {
              //  int user_id = cursor.getInt(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_USER_ID));
                String picturl = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_PICTURE_URL));
                String place_name = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_PLACE_NAME));
                String comment = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_COMMENT));
                String user_name = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_USER_NAME));
                String food_name = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_FOOD_NAME));
                String user_image = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HOME_RECYLECLER_USER_IMAGE));
                HomeLastResult result = new HomeLastResult();
                result.setPlace_name(place_name);
               result.setComment(comment);
                result.setUser_name(user_name);
                result.setFood_name(food_name);
                result.setUser_image(user_image);
                result.setPicture_url(picturl);
               // result.setUser_id(user_id);
                results.add(result);

            }
            cursor.close();
            return results;
        }


        return null;
    }
    public void deleHomeRecylerView() {
        database.delete(Constant.TABLE_NAME_HOME_RECYLECLER, null, null);
    }




}


